package com.ziyun.erp.web.api.goods;

import com.ziyun.erp.modules.goods.entity.GoodsOptionEntity;
import com.ziyun.erp.modules.goods.service.GoodsOptionService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 属性选项表
 *
 * @author xueqz
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("port/goods/goodsoption")
public class GoodsOptionControllerApi {
    @Autowired
    private GoodsOptionService goodsOptionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsOptionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{optionId}")
    public R info(@PathVariable("optionId") Long optionId){
        GoodsOptionEntity goodsOption = goodsOptionService.selectById(optionId);
        return R.ok().put("goodsOption", goodsOption);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsOptionEntity goodsOption){
        goodsOptionService.insert(goodsOption);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsOptionEntity goodsOption){
        ValidatorUtils.validateEntity(goodsOption);
//        goodsOptionService.updateAllColumnById(goodsOption);//全部更新
        goodsOptionService.updateValues(goodsOption);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] optionIds){
        goodsOptionService.deleteBatchIds(Arrays.asList(optionIds));
        return R.ok();
    }

    /**
     * 获得规格属性值
     */
    @RequestMapping("/getValues/{attributeId}")
    public R getValues(@PathVariable("attributeId") Long attributeId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("attribute_id",attributeId);
        map.put("disabled",0);
        List<GoodsOptionEntity> goodsOptionList = goodsOptionService.selectByMap(map);
        return R.ok().put("goodsOptionList",goodsOptionList);
    }

    /**
     * 信息
     */
    @RequestMapping("/flagDelete/{optionId}")
    public R flagDelete(@PathVariable("optionId") Long optionId){
        goodsOptionService.flagDeleteByOptionId(optionId);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateValue")
    public R updateValue(@RequestBody GoodsOptionEntity goodsOption){
        ValidatorUtils.validateEntity(goodsOption);
        goodsOptionService.updateValues(goodsOption);
        return R.ok();
    }


}
